package org.zjt.agent.attach;

import com.sun.tools.attach.AttachNotSupportedException;
import com.sun.tools.attach.VirtualMachine;
import com.sun.tools.attach.VirtualMachineDescriptor;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * Description:
 *
 * @author juntao.zhang
 * Date: 2018-11-01 13:48
 * @see
 */
public class AttackMain {


    public static void main(String[] args) throws IOException, AttachNotSupportedException {

        String path = "/Users/juntao.zhang/dev/tools/apache-maven-3.5.3/local-repo/caches/modules-2/files-2.1/net.bytebuddy/byte-buddy/1.7.5/baf12b76298eed6a244d9a4de6b5c3baa27ffa0e/byte-buddy-1.7.5.jar";
        JarFile localJarFile = new JarFile(new File(path));
        Enumeration<JarEntry> entries = localJarFile.entries();
        while (entries.hasMoreElements()) {
            JarEntry jarEntry = entries.nextElement();
            String innerPath = jarEntry.getName();
            System.out.println(innerPath);
        }
    }


    public static void getClassPath()throws IOException, AttachNotSupportedException {
        List<VirtualMachineDescriptor> list = VirtualMachine.list();
        for (VirtualMachineDescriptor virtualMachineDescriptor : list) {
            if (!virtualMachineDescriptor.displayName().contains("frog")){
                continue;
            }
            System.out.println(virtualMachineDescriptor.displayName());
            VirtualMachine attach = VirtualMachine.attach(virtualMachineDescriptor.id());
            Properties systemProperties = attach.getSystemProperties();

            System.out.println(systemProperties.getProperty("java.class.path"));
            systemProperties.forEach((k,v) -> System.out.println(k +" : "+v));
        }
    }

}
